#ifndef _DEV_CFG_H
#define _DEV_CFG_H



#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#if defined(__GNUC__) || defined(__clang__)
  #define DEVICE_CONSTRUCTOR __attribute__((constructor))
#elif defined(_MSC_VER)
  // MSVC 没有直接支持 constructor 属性，需用其他手段如 #pragma section 和函数指针（较复杂）
  #define DEVICE_CONSTRUCTOR
#else
  #define DEVICE_CONSTRUCTOR
#endif

#ifndef DEVICE_MALLOC
#define DEVICE_MALLOC malloc
#endif

#ifndef DEVICE_FREE
#define DEVICE_FREE free
#endif

#ifndef DEVICE_ALLOC
#define DEVICE_ALLOC malloc
#endif

#ifndef EXTERN
#define EXTERN extern
#endif

#ifndef DEVICE_PUBLIC
#define DEVICE_PUBLIC(x)    x
#endif


#ifndef DEVICE_NUM_MAX
#define DEVICE_GPIO_NUM_MAX     (8)
#define DEVICE_UART_NUM_MAX     (1)
#define DEVICE_IIC_NUM_MAX      (0)
#define DEVICE_SPI_NUM_MAX      (0)
#define DEVICE_TIMER_NUM_MAX    (0)
#define DEVICE_NUM_MAX          (DEVICE_GPIO_NUM_MAX + DEVICE_UART_NUM_MAX + DEVICE_IIC_NUM_MAX + DEVICE_SPI_NUM_MAX +\
                                 DEVICE_TIMER_NUM_MAX)
#endif

#ifndef DRIVER_NUM_MAX
#define DRIVER_GPIO_NUM_MAX     (1)
#define DRIVER_UART_NUM_MAX     (1)
#define DRIVER_IIC_NUM_MAX      (0)
#define DRIVER_SPI_NUM_MAX      (0)
#define DRIVER_TIMER_NUM_MAX    (0)
#define DRIVER_NUM_MAX          (DRIVER_GPIO_NUM_MAX + DRIVER_UART_NUM_MAX + DRIVER_IIC_NUM_MAX + DRIVER_SPI_NUM_MAX +\
                                 DRIVER_TIMER_NUM_MAX)
#endif

#ifndef BUS_NUM_MAX
#define BUS_GPIO_NUM_MAX        (1)
#define BUS_UART_NUM_MAX        (1)
#define BUS_IIC_NUM_MAX         (0)
#define BUS_SPI_NUM_MAX         (0)
#define BUS_TIMER_NUM_MAX       (0)
#define BUS_NUM_MAX             (BUS_GPIO_NUM_MAX + BUS_UART_NUM_MAX + BUS_IIC_NUM_MAX + BUS_SPI_NUM_MAX + BUS_TIMER_NUM_MAX)
#endif

#define DEVICE_GET_TRUE_VALUE(flag, value1, value2)   ((flag) ? (value1) : (value2))
#define DEVICE_GET_VALID_VALUE(val, min_valid, def) DEVICE_GET_TRUE_VALUE((val) > (min_valid), (val), (def))
#define DEVICE_MIN(a, b) ((a) < (b) ? (a) : (b)) /**< Minimum of two values */
#define DEVICE_MAX(a, b) ((a) > (b) ? (a) : (b)) /**< Maximum of two values */
#define DEVICE_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) /**< Size of an array */
#define DEVICE_IS_NULL(ptr) ((ptr) == NULL) /**< Check if pointer is NULL */
#define DEVICE_IS_NOT_NULL(ptr) ((ptr) != NULL) /**< Check if pointer is not NULL */
#define DEVICE_IS_EMPTY(str) ((str) == NULL || (str)[0] == '\0') /**< Check if string is empty */
#define DEVICE_IS_NOT_EMPTY(str) ((str) != NULL && (str)[0] != '\0') /**< Check if string is not empty */
#define DEVICE_IS_ALIGNED(addr, align) (((uintptr_t)(addr) & ((align)-1)) == 0) /**< Check if address is aligned */
#define DEVICE_UNUSED(x) (void)(x) /**< Suppress unused variable warnings */


#define DEVICE_DEBUG_OFF    (0)  /**< No logs */
#define DEVICE_ERROR_LEVEL  (1)  /**< Error level logs */
#define DEVICE_WARN_LEVEL   (2)  /**< Warning level logs */
#define DEVICE_INFO_LEVEL   (3)  /**< Info level logs */
#define DEVICE_DEBUG_LEVEL  (4)  /**< Full debug logs */

#ifndef DEVICE_DEBUG
#define DEVICE_DEBUG         (DEVICE_DEBUG_LEVEL)
#endif

#if (DEVICE_DEBUG >= DEVICE_DEBUG_LEVEL)
#define DEVICE_LOG_DEBUG(TAG, ...)  do { printf("[%s]: ", TAG); printf(__VA_ARGS__); printf("\r\n"); } while(0)
#else
#define DEVICE_LOG_DEBUG(TAG, ...)
#endif

#if (DEVICE_DEBUG >= DEVICE_INFO_LEVEL)
#define DEVICE_LOG_INFO(TAG, ...)   do { printf("[%s]: ", TAG); printf(__VA_ARGS__); printf("\r\n"); } while(0)
#else
#define DEVICE_LOG_INFO(TAG, ...)
#endif

#if (DEVICE_DEBUG >= DEVICE_WARN_LEVEL)
#define DEVICE_LOG_WARN(TAG, ...)   do { printf("[%s]: ", TAG); printf(__VA_ARGS__); printf("\r\n"); } while(0)
#else
#define DEVICE_LOG_WARN(TAG, ...)
#endif

#if (DEVICE_DEBUG >= DEVICE_ERROR_LEVEL)
#define DEVICE_LOG_ERROR(TAG, ...)  do { printf("[%s]: ", TAG); printf(__VA_ARGS__); printf("\r\n"); } while(0)
#else
#define DEVICE_LOG_ERROR(TAG, ...)
#endif


#endif
